﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Net.Mail;
using System.IO;
using Keepo.Models;
using System.Web.Mvc;

namespace AlbumDesigner.Helpers
{
    public class MailHelper
    {
        public static void SendMail(string to, string subject, string body, string albumLoginUrl = "")
        {
            try
            {
                if (!string.IsNullOrEmpty(to))
                {
                    MailMessage mail = new MailMessage();
                    mail.To.Add(to);

                    mail.From = new MailAddress("info@elinirschool.co.il");
                    mail.Subject = subject;
                    mail.IsBodyHtml = true;

                    string mailBody = File.ReadAllText(HttpContext.Current.Server.MapPath("~/MailFormat.htm"));

                    //if there is album login url to show in the mail
                    if (!string.IsNullOrEmpty(albumLoginUrl))
                    {
                        body += "<br /><br /><a href='" + albumLoginUrl + "' style='font-weight:bold;'>התחבר כעורך</a>";
                    }

                    mailBody = mailBody.Replace("{Title}", subject);
                    mailBody = mailBody.Replace("{Content}", body);
                    mail.Body = mailBody;

                    SmtpClient smtp = new SmtpClient("smtp.gmail.com");
                    smtp.UseDefaultCredentials = false;
                    smtp.Credentials = new System.Net.NetworkCredential("info@elinirschool.co.il", "elinir1978");
                    smtp.EnableSsl = true;

                    smtp.Send(mail);
                }
            }
            catch (Exception e) { }
        }


        public static KeyValuePair<bool, string> SendMsgMailToAllSectionEditors(int albumId, string mailText, UrlHelper Url)
        {
            var result = new KeyValuePair<bool, string>(true, "דואר נשלח בהצלחה לכל העורכים");

            using (var db = new AlbumContext())
            {
                var albumSections = db.AlbumSections.Where(m => m.AlbumId == albumId);

                foreach (var section in albumSections)
                {
                    var sectionResult = SendMsgMailToSectionEditor(section.AlbumSectionId, mailText, Url);

                    if (!result.Key)
                    {
                        result = sectionResult;
                    }
                }
            }

            return result;
        }

        public static KeyValuePair<bool, string> SendMsgMailToSectionEditor(int sectionId, string mailText, UrlHelper Url)
        {
            using (var db = new AlbumContext())
            {
                var albumSection = db.AlbumSections.Find(sectionId);

                var mailTitle = "הודעת מערכת לעורך - " + albumSection.Title;
                var albumLoginUrl = Url.Action("EditorLogin", "Users", new { id = albumSection.Album.OrderNum }, "http");

                if (!string.IsNullOrEmpty(albumSection.Editor.Email))
                {
                    try
                    {
                        AlbumDesigner.Helpers.MailHelper.SendMail(albumSection.Editor.Email, mailTitle, mailText, albumLoginUrl);
                    }
                    catch
                    {
                        return new KeyValuePair<bool, string>(false, "שליחת המייל נכשלה.");
                    }
                }
                else
                {
                    return new KeyValuePair<bool, string>(false, "אין כתובת דוא\"ל לעורך המקטע.");
                }

                return new KeyValuePair<bool, string>(true, "המייל נשלח בהצלחה.");
            }
        }

        public static object SendPasswordMailToAllSectionEditors(int albumId, UrlHelper Url)
        {
            var result = new KeyValuePair<bool, string>(true, "דואר נשלח בהצלחה לכל העורכים");

            using (var db = new AlbumContext())
            {
                var albumSections = db.AlbumSections.Where(m => m.AlbumId == albumId);

                foreach(var section in albumSections)
                {
                    var sectionResult = SendPasswordMailToSectionEditor(section.AlbumSectionId, Url);
                    
                    if (!result.Key)
                    {
                        result = sectionResult;
                    }
                }
            }

            return result;
        }

        public static KeyValuePair<bool, string> SendPasswordMailToSectionEditor(int sectionId, UrlHelper Url)
        {
            using (var db = new AlbumContext())
            {
                var albumSection = db.AlbumSections.Find(sectionId);

                //send mail to the Album Section Editor
                var mailTitle = "סיסמאת עריכה - " + albumSection.Title;
                var mailText = "בהודעה זו מצורפת סיסמתך האישית המשמשת לעריכת \"" + albumSection.Title + "\" באלבום \"" + albumSection.Album.Title + "\".<br />אתה מוזמן להתחבר ולערוך את המקטע.<br /><strong>סיסמת עורך " + albumSection.Title + " </strong>: " + albumSection.Editor.Password;
                var albumLoginUrl = Url.Action("EditorLogin", "Users", new { id = albumSection.Album.OrderNum }, "http");

                if (!string.IsNullOrEmpty(albumSection.Editor.Email))
                {
                    try
                    {
                        AlbumDesigner.Helpers.MailHelper.SendMail(albumSection.Editor.Email, mailTitle, mailText, albumLoginUrl);
                    }
                    catch
                    {
                        return new KeyValuePair<bool, string>(false, "שליחת המייל נכשלה.");
                    }
                }
                else
                {
                    return new KeyValuePair<bool, string>(false, "אין כתובת דוא\"ל לעורך המקטע.");
                }

                return new KeyValuePair<bool, string>(true, "המייל נשלח בהצלחה.");
            }
        }

    }
}